<?php
/*
 * @Author: lokei
 * @Date: 2022-10-24 23:37:22
 * @LastEditors: lokei
 * @LastEditTime: 2022-10-25 11:41:12
 * @Description: 
 */
namespace App\Http\Controllers\Cms\Comment;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Cms\Comment\CommentModel;
use Illuminate\Http\Request;

class CommentController extends Controller {
    public function list() {
        $items = CommentModel::get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }
    public function submit(Request $request) {
        $user = app('auth')->user();
        $comment = new CommentModel();
        $comment->uid = $user->uid;
        $comment->order_id = $request->input('order_id');
        $comment->goods_id = $request->input('goods_id');
        $comment->content = $request->input('content');
        $comment->image = $request->input('image');
        $comment->avatar = $request->input('avatar');
        $comment->name = $request->input('name');
        $comment->mobile = $request->input('mobile');
        $comment->created_at = date('Y-m-d H:i:s', time());
        $comment->save();
        return ResultTool::success();
    }

    public function ofOrder(Request $request) {
        $comment = CommentModel::where('order_id', '=', $request->input('order_id'))->first();
        $res = ResultTool::success();
        $res['data'] = $comment;
        return $res;
    }

    public function ofGoods(Request $request) {
        $items = CommentModel::where('goods_id', '=', $request->input('goods_id'))->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function delete(Request $request) {
        CommentModel::where('id', '=', $request->input('id'))->delete();
        return ResultTool::success();
    }
}